package com.jwtsecurity.config.security;

import com.alibaba.fastjson.JSON;
import com.jwtsecurity.system.entity.Userinfo ;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Component
public class SecuritySeccussHandler extends SavedRequestAwareAuthenticationSuccessHandler {
    @Override
    public void onAuthenticationSuccess
            (HttpServletRequest req, HttpServletResponse res, Authentication auth)
            throws ServletException, IOException {
        Map<String,Object> r = new HashMap<>();
        res.setContentType("application/json;charset=UTF-8");
        Userinfo principal = (Userinfo) auth.getPrincipal();
        r.put("code","200");
        r.put("msg","ok");
        r.put("href","/jwtsecurity/index");
        r.put("data",principal.getVname());
        res.getWriter().write(JSON.toJSONString(r));
    }
}
